﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Module.FlowChart.View
{
    public partial class Form_PageEditor : Form
    {
        public class PageNameEditor_Handles
        {
            public Func<string, bool> DeletPage;
            public Func<string, bool> MoveUp;
            public Func<string, bool> MoveDown;
            public Func<string, string, bool> ChangeName;
            public Func<ICollection<string>> GetPageItems;
            public Func<string, bool> CreatNewPage;
        }

        PageNameEditor_Handles Handles;
        public Form_PageEditor(PageNameEditor_Handles handles)
        {
            Handles = handles;
            InitializeComponent();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        //上移
        private void button1_Click(object sender, EventArgs e)
        {
            int sel = listBox1.SelectedIndex;
            if (sel <= 0)
                return;
            object item = listBox1.SelectedItem;
            listBox1.Items.RemoveAt(sel);
            listBox1.Items.Insert(sel - 1, item);
            Handles.MoveUp(item.ToString());
        }
        //下移
        private void button2_Click(object sender, EventArgs e)
        {
            int sel = listBox1.SelectedIndex;
            if (sel >= listBox1.Items.Count - 1 || sel < 0)
                return;
            object item = listBox1.SelectedItem;
            listBox1.Items.RemoveAt(sel);
            listBox1.Items.Insert(sel + 1, item);
            Handles.MoveDown(item.ToString());
        }
        //确认修改
        private void button4_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0)
                return;
            if (textBox1.Text.Replace(" ", "").Length == 0)
            {
                MessageBox.Show("请输入有效的名称", "错误");
                return;
            }
            if (Handles.ChangeName(listBox1.SelectedItem.ToString(), textBox1.Text))
            {
                int sel = listBox1.SelectedIndex;
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
                listBox1.Items.Insert(sel, textBox1.Text);
            }
            else
            {
                MessageBox.Show("修改失败", "错误");
                return;
            }
        }
        //删除
        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex < 0)
                return;
            if (Handles.DeletPage(listBox1.SelectedItem.ToString()))
            {
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
            }
            else
            {
                MessageBox.Show("删除失败", "错误");
                return;
            }
        }

        public void BeforShow()
        {
            textBox1.Text = "";
            listBox1.Items.Clear();
            foreach (var item in Handles.GetPageItems())
            {
                listBox1.Items.Add(item);
            }
        }

        private void listBox1_MouseClick(object sender, MouseEventArgs e)
        {
            int index = listBox1.IndexFromPoint(e.Location);
            if (index >= 0)
            {
                textBox1.Text = listBox1.Items[index].ToString();
                return;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string typename = Form_CreatePage.ShowWindow();
            Handles.CreatNewPage(typename);
            string s = Handles.GetPageItems().Last();
            listBox1.Items.Add(s);
        }
    }
}
